package com.xyd.servlet;

import com.xyd.entity.User;
import com.xyd.service.UserService;
import com.xyd.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 类描述：
 *用户管理新增
 * @author 赵瑞硕
 * @date 2021/1/12 16:02
 */

@WebServlet("/userVisitAddServlet")
public class UserVisitAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String euUserId=request.getParameter("userName");
        String euUsername=request.getParameter("name");
        String euPassword=request.getParameter("password");
        String euSex=request.getParameter("sex");
        String euBirthday=request.getParameter("birthday");
        String euIdentityCode=request.getParameter("idnumber");
        String euMobile=request.getParameter("mobile");
        String euEmail=request.getParameter("email");
        String euAddress=request.getParameter("address");
        Integer euStatus=Integer.parseInt(request.getParameter("status"));
        boolean flag=false;

        UserService userService=new UserServiceImpl();
        boolean flags= userService.isHasUsername(euUsername);
        if (flags){
            response.sendRedirect("userVisitServlet?addname=yes");
        }
        else {
            User user=new User(euUserId,euUsername,euPassword,euSex,euBirthday,euIdentityCode,euEmail,euMobile,euAddress,euStatus);
            flag=userService.registerUser(user);
            if (flag){
                request.getRequestDispatcher("userVisitServlet").forward(request,response);
            }
            else {
                response.sendRedirect("userVisitServlet?add=yes");
            }
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
